#!/bin/sh
#
# nsdaemon         Start/Stop nsdaemon
#
# chkconfig: 345 66 39
# description: NS server daemon
#
# nsdaemon.scripts,v 1.7 2004/03/09 19:11:59 jdurand Exp
#
# @(#)nsdaemon.scripts,v 1.7 2004/03/09 19:11:59 CERN/IT/ADC/CA Jean-Damien Durand
#

#
## PLEASE USE sysconfig FILE TO CHANGE THESE ENVIRONMENT VARIABLES
#
export PATH=${PATH}:/usr/local/bin

#
## Try to guess about oracle environment - cross your fingers or use sysconfig
#
DBHOME=`which dbhome 2>/dev/null`
if [ -n "$DBHOME" ]; then
  export ORACLE_HOME=`dbhome CASTOR`
else
  # Cross your fingers
  export ORACLE_HOME=/oracle/product/castor
fi
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$ORACLE_HOME/lib
export ORACLE_SID=CASTOR
export PATH=${PATH}:${ORACLE_HOME}/bin

sysname=`uname -s`

# source function library
if [ -r /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
	DAEMON=daemon
	FAILURE=failure
	ECHO_FAILURE=failure
	SUCCESS=success
	ECHO_SUCCESS=echo_success
	ECHO_END=echo
	if [ $sysname = "Linux" ]; then
		ECHOOPT=-n
	fi
else
	DAEMON=
	FAILURE=
	ECHO_FAILURE=
	SUCCESS=
	ECHO_SUCCESS=
	ECHOOPT=
	ECHO_END=
fi

RETVAL=0
prog="nsdaemon"
PIDFILE=/var/run/nsdaemon.pid
PIDDIR=/var/run
SUBSYS=/var/lock/subsys/nsdaemon
SUBSYSDIR=/var/lock/subsys
[ -z "$SILENTSTOP" ] && SILENTSTOP=0
NSDAEMON=/usr/local/bin/nsdaemon
NSSHUTDOWN=/usr/local/bin/nsshutdown
# Not yet supported
NSDAEMONLOGFILE=/var/log/nsdaemon
OPTIONS=""
SYSCONFIGNSDAEMON=/etc/sysconfig/nsdaemon

#
## Blindly attempt to create useful directories
#
[ ! -d $PIDDIR ] && mkdir -p $PIDDIR >& /dev/null
[ ! -d $SUBSYSDIR ] && mkdir -p $SUBSYSDIR >& /dev/null

if [ $sysname = "HP-UX" ]; then
	export UNIX95=1
fi
if [ $sysname = "SunOS" ]; then
	format4comm="fname"
else
	format4comm="comm"
fi

case "$1" in
  start)
	echo $ECHOOPT "Starting $prog: "
	if [ $sysname = "Linux" -a `whoami` = "root" ]; then
        netstat -ap | egrep '\*:5010|\*:cns' | grep LISTEN | grep nsdaemon > /dev/null
    else
        netstat -a  | egrep '\*:5010|\*:cns' | egrep 'LISTEN *$' > /dev/null
    fi
	# ps -eo ppid,$format4comm | egrep '^[ \t]+1 nsdaemon' > /dev/null
	if [ $? -eq 0 ]; then
		echo $ECHOOPT "nsdaemon already started: "
		[ -n "$FAILURE" ] && $FAILURE "nsdaemon already started: "
		RETVAL=1
	else
        if [ -r $SYSCONFIGNSDAEMON ]; then
            #
            ## Source the configuration
            #
            . $SYSCONFIGNSDAEMON
            if [ "${RUN_NSDAEMON}" != "yes" ]; then
                echo $ECHOOPT "$SYSCONFIGNSDAEMON says NO: "
                [ -n "$FAILURE" ] && $FAILURE "$SYSCONFIGNSDAEMON says NO: "
                RETVAL=1
            fi
            if [ -n "${ULIMIT_N}" ]; then
                ulimit -n ${ULIMIT_N}
            fi
        fi
        if [ $RETVAL -eq 0 ]; then
            cd /
            mkdir -p /usr/spool/ns
            $DAEMON $NSDAEMON $OPTIONS
            if [ $? -eq 0 ]; then
                [ -d $SUBSYSDIR ] && touch $SUBSYS
                if [ -d $PIDDIR ]; then
                    pid=`ps -eo pid,ppid,$format4comm | grep " 1 nsdaemon" | awk '{print $1}'`
                    # The point of $PIDFILE is that it kills only
                    # the master daemon.
                    rm -f $PIDFILE
                    echo $pid > $PIDFILE
                fi
                RETVAL=0
            else
                echo $ECHOOPT "nsdaemon not started: "
                [ -n "$FAILURE" ] && $FAILURE "nsdaemon not started: "
                RETVAL=1
            fi
        fi
	fi
	$ECHO_END
	;;
  stop)
    CNS_HOST=`hostname`
    export CNS_HOST
	if [ -f $PIDFILE ]; then
		[ $SILENTSTOP -eq 0 ] && echo $ECHOOPT "Stopping $prog: "
        if [ -x $NSSHUTDOWN ]; then
            if [ $SILENTSTOP -eq 0 ]; then
                $NSSHUTDOWN -f -h `hostname`
            else
                $NSSHUTDOWN -f -h `hostname` >& /dev/null
            fi
            RETVAL=$?
            if [ $RETVAL -ne 0 ]; then
                if [ $SILENTSTOP -eq 0 ]; then
                    kill -2 -`cat $PIDFILE`
                else
                    kill -2 -`cat $PIDFILE` >& /dev/null
                fi
                RETVAL=$?
            fi
        else
            if [ $SILENTSTOP -eq 0 ]; then
                kill -2 -`cat $PIDFILE`
            else
                kill -2 -`cat $PIDFILE` >& /dev/null
            fi
            RETVAL=$?
        fi
        if [ $RETVAL -eq 0 ]; then
            rm -f $PIDFILE
        fi
	else
		# nsdaemon might have been started by hand
		pid=`ps -eo pid,ppid,$format4comm | grep " 1 nsdaemon" | awk '{print $1}'`
		if [ -n "$pid" ]; then
			[ $SILENTSTOP -eq 0 ] && echo $ECHOOPT "Stopping $prog: "
            if [ -x $NSSHUTDOWN ]; then
                if [ $SILENTSTOP -eq 0 ]; then
                    $NSSHUTDOWN -f -h `hostname`
                else
                    $NSSHUTDOWN -f -h `hostname` >& /dev/null
                fi
                RETVAL=$?
                if [ $RETVAL -ne 0 ]; then
                    if [ $SILENTSTOP -eq 0 ]; then
                        kill -2 -$pid
                    else
                        kill -2 -$pid >& /dev/null
                    fi
                    RETVAL=$?
                fi
            else
                if [ $SILENTSTOP -eq 0 ]; then
                    kill -2 -$pid
                else
                    kill -2 -$pid >& /dev/null
                fi
                RETVAL=$?
            fi
		else
			[ $SILENTSTOP -eq 0 ] && echo $ECHOOPT "nsdaemon already stopped: "
			[ $SILENTSTOP -eq 0 -a -n "$FAILURE" ] && $FAILURE "nsdaemon already stopped: "
 			RETVAL=1
		fi
	fi
	if [ $SILENTSTOP -eq 0 -a $sysname = "Linux" ]; then
		if [ $RETVAL -eq 0 ]; then
 			$ECHO_SUCCESS
		else
 			$ECHO_FAILURE
		fi
	fi
	[ -d $SUBSYSDIR ] && rm -f $SUBSYS
	[ $SILENTSTOP -eq 0 -a -n "$ECHO_END" ] && $ECHO_END
    #
    ## Basically: if SILENTSTOP is not 0 when we never print something
    ## But if the stop is successful... One might want to see stop: OK
    ## anyway ?
    #
	if [ $SILENTSTOP -ne 0 -a $RETVAL -eq 0 ]; then
        echo $ECHOOPT "Stopping $prog: "
        [ $sysname = "Linux" ] && $ECHO_SUCCESS
        [ -n "$ECHO_END" ] && $ECHO_END
    fi
	;;
  restart)
	$0 stop
	if [ $? -eq 0 -o $SILENTSTOP -eq 0 ]; then
		$0 start
		RETVAL=$?
	else
        #
        ## It is legal to return zero it if was
        ## a conditional restart
        #
        if [ $SILENTSTOP -eq 0 ]; then
            RETVAL=1
        else
            RETVAL=0
        fi
	fi
	;;
  condrestart)
	SILENTSTOP=1
    export SILENTSTOP
	$0 restart
    RETVAL=$?
	;;
  status)
	pid=`ps -eo pid,ppid,$format4comm | grep " 1 nsdaemon" | awk '{print $1}'`
	if [ -n "$pid" ]; then
		echo $ECHOOPT "nsdaemon (pid $pid) is running..."
		$ECHO_SUCCESS
		$ECHO_END
	else
		if [ -f $PIDFILE ]; then
			pid=`head -1 $PIDFILE`
			if [ "$pid" != "" ] ; then
				echo $ECHOOPT "nsdaemon dead but pid file exists"
				$ECHO_FAILURE
				$ECHO_END
			fi
		else
			if [ -f $SUBSYS ]; then
				echo $ECHOOPT "nsdaemon dead but subsys ($SUBSYS) locked"
			else
				echo $ECHOOPT "nsdaemon is stopped"
			fi
			$ECHO_FAILURE
			$ECHO_END
		fi
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|condrestart}"
	RETVAL=1
	;;
esac

exit $RETVAL
